<script setup lang="ts">
//引入组件
import { getHomeBannerAPI, getHomeCategoryAPT, grtHomeHotAPI } from '@/services/home'
import CustomNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import PageSkeleton from './components/PageSkeleton.vue'
import { useGuessList } from '@/composables'

// 获取轮播图数据
const bannerList = ref<BannerItem[]>([])
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  bannerList.value = res.result
}

// 获取小程序首页分类数据
const categoryList = ref<CategoryItem[]>([])
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPT()
  categoryList.value = res.result
}

// 获取热门推荐代码
const homeHotList = ref<HotItem[]>([])
const getHotItemData = async () => {
  const res = await grtHomeHotAPI()
  homeHotList.value = res.result
}

// const guessRef = ref<XtxGuessInstance>()
// const onScolltolower = () => {
//   guessRef.value?.getMore()
// }

//获取猜你喜欢实例用的
const { guessRef, onScrolltolower } = useGuessList()

//下拉刷新
const isTriggered = ref(false)
const onRefresherrefresh = async () => {
  isTriggered.value = true
  guessRef.value?.resetData()
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHotItemData()])
  guessRef.value?.getMore()
  isTriggered.value = false
}

// 是否加载中的标记
const isLoading = ref(false)

onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHotItemData()])
  isLoading.value = false
})
</script>

<template>
  <CustomNavbar></CustomNavbar>
  <scroll-view
    :refresher-enabled="true"
    @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isTriggered"
    @scrolltolower="onScrolltolower"
    scroll-y
    class="scroll-view"
  >
    <!-- 骨架屏 -->
    <PageSkeleton v-if="isLoading" />
    <template v-else>
      <!-- 轮播图 -->
      <XtxSwiper :list="bannerList"></XtxSwiper>
      <!-- 自定义推荐 -->
      <CategoryPanel :list="categoryList" />
      <!-- 热门推荐 -->
      <HotPanel :list="homeHotList" />
      <XtxGuess ref="guessRef" />
    </template>

    <view>index</view>
  </scroll-view>
</template>

<style lang="scss">
//
page {
  background-color: #f7f7ff;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
</style>
